package com.example.neusoft.yuejian_12.Model;

import com.example.neusoft.yuejian_12.Interface.TrainingFace;
import com.example.neusoft.yuejian_12.JavaBean.Training;
import com.example.neusoft.yuejian_12.Listener.TrainingDetailListener;
import com.example.neusoft.yuejian_12.Listener.TrainingListener;
import com.example.neusoft.yuejian_12.Listener.TrainingSignlListener;
import com.example.neusoft.yuejian_12.Service.TrainingService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by admin on 2017/9/30.
 */

public class TrainingModel implements TrainingFace {
    private Retrofit retrofit;
    private TrainingService trainingService;
   private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址*/

    public TrainingModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void TrainingAllList(String id, String training_name, final TrainingListener listListener) {
        trainingService = retrofit.create(TrainingService.class);
        Call<List<Training>> call =trainingService.getAllTrainingList();
        call.enqueue(new Callback<List<Training>>() {
            @Override
            public void onResponse(Call<List<Training>> call, Response<List<Training>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<Training>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void TrainingCList(String SessionID, String degree, final TrainingListener listListener) {
        trainingService = retrofit.create(TrainingService.class);
        Call<List<Training>> call =trainingService.getTrainingList("lgj98rfkiqvenhrk2o7oe2po81","train",degree);
        call.enqueue(new Callback<List<Training>>() {
            @Override
            public void onResponse(Call<List<Training>> call, Response<List<Training>> response) {
                if (response.isSuccessful() && response.body() != null) {

                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<Training>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
    @Override
    public void getdetail(String id, final TrainingDetailListener listListener) {
        trainingService = retrofit.create(TrainingService.class);
        Call<Training> call =trainingService.gettrainbyid(id);
        call.enqueue(new Callback<Training>() {
            @Override
            public void onResponse(Call<Training> call, Response<Training> response) {
                if (response.isSuccessful() && response.body() != null) {

                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<Training> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
    @Override
    public void signTrain(String trname,String train_id,String telephone,String Anum,String Pay_type,
                          String pay,String notice,int userid, final TrainingSignlListener listListener) {
        trainingService = retrofit.create(TrainingService.class);
        Call<Integer> call =trainingService.signTrain("signtrain",trname,train_id,telephone,Anum,Pay_type,pay,notice,userid,"lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()!=0){
                    listListener.onResponse(response.body());
                }
                else{
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

   /* public Call<Integer> signTrain(String trname,String train_id,String telephone,String Anum,String Pay_type,
                                   String pay,String notice,int userid){
        TrainingService trainingService = retrofit.create(TrainingService.class);
        return trainingService.signTrain("signtrain",trname,train_id,telephone,Anum,Pay_type,pay,notice,userid,"lgj98rfkiqvenhrk2o7oe2po81");
    }*/
}
